package com.imdroid.carac.activities;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

import com.imdroid.carac.IMDroid;
import com.imdroid.carac.R;

/**
 * 行车记录
 * 
 */
public class RecordActivity extends Activity {
	// private final String TAG = "RecordActivity";
	// 用作状态变量，准备就绪时为本意，null表示本activity停止
	public static boolean running;
	static SurfaceView surfaceView;
//	private Button buttonPicture;
	private boolean keepBackEndRecord = false;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	public void onCreate(Bundle bdl) {
		// 初始化视图
		super.onCreate(bdl);
		getWindow().setFormat(PixelFormat.TRANSLUCENT);
		setContentView(R.layout.record);
//		buttonPicture = (Button) findViewById(R.id.buttonPicture);
//		buttonPicture.setKeepScreenOn(true);

		// 进入后讲后台录像的停止，悬浮窗口隐藏
		FloatView.show();
		running = true;
		Record.stop();
		Record.initParams(); // 每次重进大屏录像时，清空手工改过的摄像头参数。
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onStop()
	 */
	protected void onStop() {
		super.onStop();
		if (!keepBackEndRecord)
			Record.stop();
		running = false;
		surfaceView = null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onWindowFocusChanged(boolean)
	 */
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		if (!hasFocus)
			return;
		surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
		Record.stop();
		Record.start();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onBackPressed()
	 */
	public void onBackPressed() {
		if (IMDroid.getRecordBackground())
			FloatView.show();
		else
			FloatView.hide();
		setResult(RESULT_OK, new Intent(this, RecordMenuActivity.class));
		finish();
	}

	/**
	 * Clicking the button of return
	 * 
	 * @param view
	 *            the buttong of review
	 */
	public void onButtonReturn(View view) {
		onBackPressed();
	}

	/**
	 * Clicking the button of backend record
	 * 
	 * @param view
	 *            the button of backend record
	 */
	public void onButtonBackendRecord(View view) {
		IMDroid.setRecordBackground(true);
		keepBackEndRecord = true;
		FloatView.show();
		Record.stop();
		Record.start();
		setResult(RESULT_OK, new Intent(this, RecordMenuActivity.class));
		finish();
	}

	/**
	 * Clicking the button of take picture
	 * 
	 * @param view
	 *            the button of taking picture
	 */
	public void onButtonPicture(View view) {
		Record.takePicture();
	}

	/**
	 * Clicking the button of the vidio window size
	 * 
	 * @param view
	 */
	public void onButtonVideoWindowSize(View view) {
		Button button = (Button) findViewById(R.id.buttonVideoWindowSize);
		button.setText(String.format("%s\n%s", button.getText().toString()
				.split("\n")[0], Record.changeRecordResolution()));
	}

	/**
	 * Clicking the button to adjues the FPS
	 * 
	 * @param view
	 *            the buttong Fps
	 */
	public void onButtonFps(View view) {
		Button button = (Button) findViewById(R.id.buttonFps);
		button.setText(String.format("%s\n%s", button.getText().toString()
				.split("\n")[0], Record.changeFps()));
	}

	/**
	 * Clicking the button
	 * 
	 * @param name
	 *            The of the parameters
	 * @param rid
	 *            the id of the button
	 */
	private void onButton(String name, int rid) {
		Button button = (Button) findViewById(rid);
		button.setText(String.format("%s\n%s", button.getText().toString()
				.split("\n")[0], Record.changeCameraParam(name)));
	}

	/**
	 * Cliking the button of brightness
	 * 
	 * @param view
	 *            the botton of brightness
	 */
	public void onButtonBrightness(View view) {
		onButton("brightness", R.id.buttonBrightness);
	}

	/**
	 * Clicking the button of adjust contrast
	 * 
	 * @param view
	 *            the button contrast
	 */
	public void onButtonContrast(View view) {
		onButton("contrast", R.id.buttonContrast);
	}

	/**
	 * Clicking the button to adjust the ISO
	 * 
	 * @param view
	 *            clicking the button to adjust the iso
	 */
	public void onButtonIso(View view) {
		onButton("iso-speed", R.id.buttonIso);
	}

	/**
	 * Clicking the buttong to adjust the exposure value
	 * 
	 * @param view
	 *            the button of exposure value
	 */
	public void onButtonExposureValue(View view) {
		onButton("exposure", R.id.buttonExposureValue);
	}

	/**
	 * Click the button to adjust toe AE
	 * 
	 * @param view
	 *            clicking the button of AE
	 */
	public void onButtonAe(View view) {
		onButton("ae-mode", R.id.buttonAe);
	}

	/**
	 * Clicking the adjust the exposeure compensation
	 * 
	 * @param view
	 *            Clicking the button button exposeure compensation
	 */
	public void onButtonExposureCompensation(View view) {
		Button button = (Button) findViewById(R.id.buttonExposureCompensation);
		button.setText(String.format("%s\n%s", button.getText().toString()
				.split("\n")[0], Record.changeCameraExposureCompensation()));
	}

}
